/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package efrei.ete.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Frank
 */
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doPost(httpServletRequest, httpServletResponse);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //analysing request get "email" and "password"
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        
        

        JSONObject returnMsg = new JSONObject();    //used to generating return msg
        try {
            if(this.isAuthorPass(email, password)){  //validating email and password
                returnMsg.append("isPass", "TRUE");
            }else{
                returnMsg.append("isPass", "FALSE");
            }
        } catch (JSONException ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        response.setContentType("application/json; charset=utf-8");
        //clean the buffer
        response.setHeader("pragma", "no-cache");
        response.setHeader("cache-control", "no-cache");
        //sever recall
        PrintWriter out = response.getWriter();
        out.print(returnMsg.toString());
        out.close();
    }
    
    /**
     * method used to validating authority of the user
     * @param email
     * @param password
     * @return whether it is passed
     */
    private boolean isAuthorPass(String email, String password){
        return true;
    }
}
